package com.duoqin.account;

import android.accounts.AbstractAccountAuthenticator;
import android.accounts.Account;
import android.accounts.AccountAuthenticatorResponse;
import android.accounts.NetworkErrorException;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;


public class DuoqinAccountAuthenticator extends AbstractAccountAuthenticator {
    protected static final String TAG = "duoqin";
    private Context mContext;

    public DuoqinAccountAuthenticator(Context context) {
        super(context);
        mContext = context;
    }

    @Override
    public Bundle editProperties(AccountAuthenticatorResponse response, String accountType) {
        return null;
    }

    @Override
    public Bundle addAccount(AccountAuthenticatorResponse response, String accountType,
                             String authTokenType, String[] requiredFeatures, Bundle options)
            throws NetworkErrorException {
        Log.e(TAG, "Authenticator addAccount");
        return null;
        /*
        final Intent intent = new Intent(mContext, LoginActivity.class);
        intent.putExtra(DuoqinAccount.DUOQIN_ACCOUNT_TYPE, accountType);
        intent.putExtra(DuoqinAccount.DUOQIN_ACCOUNT_TOKEN_TYPE, authTokenType);
        intent.putExtra(AccountManager.KEY_ACCOUNT_AUTHENTICATOR_RESPONSE, response);

        final Bundle bundle = new Bundle();
        bundle.putParcelable(AccountManager.KEY_INTENT, intent);

        return bundle;
        */
    }

    @Override
    public Bundle confirmCredentials(AccountAuthenticatorResponse response, Account account,
                                     Bundle options) throws NetworkErrorException {
        return null;
    }

    @Override
    public Bundle getAuthToken(AccountAuthenticatorResponse response, Account account,
                               String authTokenType, Bundle options) throws NetworkErrorException {
        Log.e(TAG, "Authenticator getAuthToken");
        return null;
        /*
        AccountManager am = AccountManager.get(mContext);
        String authToken = am.peekAuthToken(account, authTokenType);
//        if (TextUtils.isEmpty(authToken)) {
//            RefreshTokenResponse refreshTokenResponse = refreshToken(authToken);
//            if (refreshTokenResponse != null) {
//                if (BaseViewModel.SERVER_RESULT_SUCCESS == refreshTokenResponse.getCode()) {
//                    if (refreshTokenResponse.getData() != null) {
//                        authToken = refreshTokenResponse.getData().getAccessToken();
//                    }
//                }
//            }
//        }
//
        if (!TextUtils.isEmpty(authToken)) {
            final Bundle result = new Bundle();
            result.putString(AccountManager.KEY_ACCOUNT_NAME, account.name);
            result.putString(AccountManager.KEY_ACCOUNT_TYPE, account.type);
            result.putString(AccountManager.KEY_AUTHTOKEN, authToken);
            return result;
        }

        final Intent intent = new Intent(mContext, LoginActivity.class);
        intent.putExtra(DuoqinAccount.DUOQIN_ACCOUNT_TYPE, account.type);
        intent.putExtra(AccountManager.KEY_ACCOUNT_AUTHENTICATOR_RESPONSE, response);
        Bundle retBundle = new Bundle();
        retBundle.putParcelable(AccountManager.KEY_INTENT, intent);
        return retBundle;
        */
    }

    @Override
    public String getAuthTokenLabel(String authTokenType) {
        return null;
    }

    @Override
    public Bundle updateCredentials(AccountAuthenticatorResponse response, Account account,
                                    String authTokenType, Bundle options)
            throws NetworkErrorException {
        return null;
    }

    @Override
    public Bundle hasFeatures(AccountAuthenticatorResponse response, Account account,
                              String[] features) throws NetworkErrorException {
        return null;
    }

    /*
    private RefreshTokenResponse refreshToken(String token) {
        Log.d(TAG, "refreshToken");
        final Call<RefreshTokenResponse> call = mApiInterface.refreshToken(token);
        RefreshTokenResponse refreshTokenResponse = null;
        try {
            Response<RefreshTokenResponse> response = call.execute();
            if (response != null) {
                Log.d(TAG, "body " + response.body());
                if (200 == response.code()) {
                    refreshTokenResponse = response.body();
                } else {
                    try {
                        String errorString = response.errorBody().string();
                        Log.w(TAG, "error " + errorString);
                        JSONObject jsonObject = new JSONObject(errorString);
                        refreshTokenResponse = new RefreshTokenResponse();
                        refreshTokenResponse.setCode(jsonObject.getInt("code"));
                        refreshTokenResponse.setMsg(jsonObject.getString("msg"));
                    } catch (IOException | JSONException e) {
                        e.printStackTrace();
                    }
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return refreshTokenResponse;
    }
    */
}
